package com.sharemarking.application.common.service;



import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.catalina.connector.ClientAbortException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.entity.OwnQusetion;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.OwnQusetionRepo;
import com.sharemarking.application.common.util.AnalysisApk;
import com.sharemarking.application.common.util.Util;

@Controller
@RequestMapping("/own")
public class PhoneOwnController {
	
	 @Autowired
	    private EmployeeRepo dao;
	 @Autowired
	 	private OwnQusetionRepo dao2;
	 	
	 
	    
	//修改客户资料
	    @RequestMapping("/changePeople")
	    @ResponseBody
	    public String changePeople( String loginName,String value,String type)throws  IOException , NoSuchAlgorithmException{
	    	
	    	String result = "";
	    	
	        Employee employeeEntity =dao.findByLoginName(loginName);
	        //System.out.println(loginName);
	        if (employeeEntity !=null){
	        	if(type.equals("X1")){
	        		employeeEntity.setName(value);
	        		dao.save(employeeEntity);
	        		result = "true";
	            	return result;
	        	}else if(type.equals("X2")){
	        		employeeEntity.setAge(value);
	        		dao.save(employeeEntity);
	        		result = "true";
	            	return result;
	        	}else if(type.equals("X3")){
	        		employeeEntity.setSex(value);
	        		dao.save(employeeEntity);
	        		result = "true";
	            	return result;
	        	}else if(type.equals("X4")){
	        		employeeEntity.setQq(value);
	        		dao.save(employeeEntity);
	        		result = "true";
	            	return result;
	        	}else if(type.equals("X5")){
	        		employeeEntity.setPhone(value);
	        		dao.save(employeeEntity);
	        		result = "true";
	            	return result;
	        	}else if(type.equals("X6")){
	        		employeeEntity.setEmail(value);
	        		dao.save(employeeEntity);
	        		result = "true";
	            	return result;
	        	}else if(type.equals("X7")){
	        		employeeEntity.setAddress(value);
	        		dao.save(employeeEntity);
	        		result = "true";
	            	return result;
	        	}
	        	
	        	
	        }else 
	        	result = "false";
	        	
	        	return result;
	        
	       
	        
	    }
	    
	  //修改头像
	    @RequestMapping("/changeImg")
	    @ResponseBody
	    public String changeImg(String loginName,String Data)throws  IOException, NoSuchAlgorithmException{
	    	
	    	String result = "";
	    	System.out.println(Data);
	    	System.out.println(loginName);
	        Employee employeeEntity =dao.findByLoginName(loginName);
	        if (employeeEntity !=null){
	        	//获取classes下的路径
                String path = this.getClass().getClassLoader().getResource("").getPath()+"public/uploadImage/peopleLogo/";
                
               //图片命名为8位随机数+loginName+Data.png
        		Date now = new Date(); 
        		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
        		String Date = dateFormat.format( now );
        		StringBuilder str=new StringBuilder();//定义变长字符串
        		Random random=new Random();
        		//随机生成数字，并添加到字符串
        		for(int i=0;i<8;i++){
        		    str.append(random.nextInt(10));
        		}
        	    
                String path2=path+str.toString()+loginName+Date+".png";
                String path3=str.toString()+loginName+Date+".png";
                String imgName=employeeEntity.getImgUrl();
               // File file = new File(path+imgName);
                //file.delete();//删除原来图片
                File folder = new File(path);
        		File[] files = folder.listFiles();
        		for(File file:files){
        			if(file.getName().equals(imgName)){
        				file.delete();
        			}
        		}
        		
                boolean img=Util.GenerateImage(Data,path2);//图片生成
                if(img){
                	 employeeEntity.setImgUrl(path3);
                     java.sql.Date createDate = new java.sql.Date(System.currentTimeMillis());//当前时间
                     employeeEntity.setUpdateBy(1);
                     employeeEntity.setUpdateDate(createDate);
                     
                     dao.save(employeeEntity);
                     
             		result = "true";
                 	return result;
                }else{
	                result = "false";	        	
		        	return result;
                }
	        }else 
	        	result = "false";
	        	
	        	return result;
	    }
	    
	    //修改头像
	    @RequestMapping("/changeImage")
	    @ResponseBody
	    public String changeImage(String loginName,String Data)throws  IOException{
	    	
	    	String result = "";
	    	System.out.println(Data);
	    	System.out.println(loginName);
	        Employee employeeEntity =dao.findByLoginName(loginName);
	        if (employeeEntity !=null){
	        	//获取classes下的路径
                String path = this.getClass().getClassLoader().getResource("").getPath()+"public/uploadImage/peopleLogo/";
                
               //图片命名为8位随机数+loginName+Data.png
        		Date now = new Date(); 
        		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
        		String Date = dateFormat.format( now );
        		StringBuilder str=new StringBuilder();//定义变长字符串
        		Random random=new Random();
        		//随机生成数字，并添加到字符串
        		for(int i=0;i<8;i++){
        		    str.append(random.nextInt(10));
        		}
        	    
                String path2=path+str.toString()+loginName+Date+".png";
                String path3=str.toString()+loginName+Date+".png";
                String imgName=employeeEntity.getImgUrl();
               
               /* File folder = new File(path);
        		File[] files = folder.listFiles();
        		for(File file:files){
        			if(file.getName().equals(imgName)){
        				file.delete();
        			}else{
        				
        			}
        		}*/
        		
                boolean img=Util.GenerateImage(Data,path2);//图片生成
                if(img){
                	 File file = new File(path+imgName);
                     file.delete();//删除原来图片
                	 employeeEntity.setImgUrl(path3);
                     java.sql.Date createDate = new java.sql.Date(System.currentTimeMillis());//当前时间
                     employeeEntity.setUpdateBy(1);
                     employeeEntity.setUpdateDate(createDate);
                     
                     dao.save(employeeEntity);
                     
             		result = "true";
                 	return result;
                }else{
	                result = "false";	        	
		        	return result;
                }
	        }else 
	        	result = "false";
	        	
	        	return result;
	    }
	
	    //问题提交
	    @RequestMapping("/sendQusetion")
	    @ResponseBody
	    public String sendQusetion(String loginName,String massage,String Data,Integer point,String line)throws  IOException{
	    	
	    	 String result = "";
	    	 String path = this.getClass().getClassLoader().getResource("").getPath()+"public/uploadImage/question/";
	         String path4 = this.getClass().getClassLoader().getResource("").getPath()+"public/uploadImage/question";
	         File file = new File(path4);
	         file.mkdirs();
	         
	        //图片命名为loginName+Data+8位随机数.png
	 		Date now = new Date(); 
	 		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
	 		String Date = dateFormat.format( now );
	 		StringBuilder str=new StringBuilder();//定义变长字符串
	 		Random random=new Random();
	 		//随机生成数字，并添加到字符串
	 		for(int i=0;i<8;i++){
	 		    str.append(random.nextInt(10));
	 		}
	 	    
	         String path2=path+loginName+Date+str.toString()+".png";
	         String path3=loginName+Date+str.toString()+".png";
	         OwnQusetion qusetion=new OwnQusetion();
	         Employee employeeEntity =dao.findByLoginName(loginName);
	         
	         qusetion.setMassage(massage);
	         qusetion.setPoint(point);
	         
	         qusetion.setLine(line);
	         qusetion.setCreateBy(0);
	         java.sql.Date createDate = new java.sql.Date(System.currentTimeMillis());//当前时间
	         qusetion.setCreateDate(createDate);
	         qusetion.setUpdateBy(1);
	         qusetion.setState("未处理");
	         qusetion.setUpdateDate(createDate);
	         qusetion.setEmployee(employeeEntity);
	         if (Data == null||Data.equals("")){
	        	 qusetion.setImgurl("");
	        	 dao2.save(qusetion);
        	 
	        	 result = "true";
          		 return result;
	         }else{
	         boolean img=Util.GenerateImage(Data,path2);//图片解析
	            if(img){
	            	qusetion.setImgurl(path3);
	            	dao2.save(qusetion);
	            	 
	                result = "true";
	             	return result;
	            }else{
	            	result = "false";
	            	return result;
	            }
	            
	         }
	    }
	  //软件更新
	    @RequestMapping("/update")
	    @ResponseBody
	    public String update(String version,String client) throws ClientAbortException,IOException{//client是手机系统
	    	 String result = "";
	    	//获取classes下的路径
	         String path = this.getClass().getClassLoader().getResource("").getPath()+"public/update/";
	         String apkUrl="";
	         String apk="";
	        
	         File file=new File(path);
	         String [] fileName = file.list();
	        
	         for (int i = 0; i < fileName.length; i++) {
	        	 apk=fileName[i];
	        	 apkUrl=path+apk;
	         }
	         
	    	 AnalysisApk.unZip(apkUrl);
	    	 String v=AnalysisApk.getVersion();
	    	 System.out.println("版本号"+v);
	    	 System.out.println("版本号"+version);
	    	
	    	 if(version.equals(v)){
	    		 result = "false";
	           	return result;
	    	 }else{
	    		 result = apk;
	    		 System.out.println(apk);
	           	return result;
	           	
	    	 }
	    	 
	    }    
	    
	  //获得最新更新版本
	    @RequestMapping("/getUpdate")
	    @ResponseBody
	    public String geUpdate() throws ClientAbortException,IOException{//client是手机系统
	    	 String result = "";
	    	//获取classes下的路径
	         String path = this.getClass().getClassLoader().getResource("").getPath()+"public/update/";
	        
	         String apk="";
	        
	         File file=new File(path);
	         String [] fileName = file.list();
	        
	         for (int i = 0; i < fileName.length; i++) {
	        	 apk=fileName[i];
	        	 
	         }
	         
	    		 result = apk;
	    		 
	           	return result;
	      
	    } 
	    
	    
}
